package m;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import m.framework.exception.SystemException;
import m.framework.utils.FileUtil;
import m.framework.utils.StrBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author wanyongming
 * @date 2011-9-26 上午11:50:05
 */
public enum WorkingEnvironment {
	
	DEV, BETA, PROD;
	private static Logger log = LoggerFactory.getLogger(WorkingEnvironment.class);
	private static WorkingEnvironment current;
	static {
		setCrrent();
	}

	private static void setCrrent() {
		//开发模式运行
		if(Config.isDevModal()){
			current=WorkingEnvironment.DEV;
			return;
		}
		
		printIp();
		
		Enumeration<NetworkInterface> netInterfaces = null;
		try {
			netInterfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			throw new SystemException("获得网络信息时出错", e);
		}
		while (netInterfaces.hasMoreElements()) {
			NetworkInterface ni = netInterfaces.nextElement();
			Enumeration<InetAddress> ips = ni.getInetAddresses();
			while (ips.hasMoreElements()) {
				String tempIp = ips.nextElement().getHostAddress();
				log.info("服务器IP:"+tempIp);
				WorkingEnvironment wo = getByIp(tempIp);
				if (wo != null) {
					wo.setIp(tempIp);
					current = wo;
					printMessage(current);
					return;
				}
			}
		}
		String text=FileUtil.getResource("/"+Config.SYSTEM_CONFIG);
		log.warn("配置文件内容:"+text);
		throw new SystemException("无法获取当前运行环境信息,请确认以上systemConfig.properties配置是否正确");
	}
	
	private static void printIp() {
		log.debug("==============打印服务器ip===============");
		Enumeration<NetworkInterface> netInterfaces = null;
		try {
			netInterfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			throw new SystemException("获得网络信息时出错", e);
		}
		while (netInterfaces.hasMoreElements()) {
			NetworkInterface ni = netInterfaces.nextElement();
			Enumeration<InetAddress> ips = ni.getInetAddresses();
			while (ips.hasMoreElements()) {
				log.info(ips.nextElement().getHostAddress());
			}
		}
	}

	private static void printMessage(WorkingEnvironment current){
		log.info("===================获取服务器信息成功,当前环境为"+current+"=========================");
		StrBuilder sb=new StrBuilder();
		sb.append("\n本机Ip:"+current.getIp());
		log.info(sb.toString()+"\n====================================================================");
	}
	
	private String ip;

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	private static WorkingEnvironment getByIp(String ip) {
		String devIp = Config.getDevIp();
		String betaIp = Config.getBetaIp();
		String prodIp = Config.getProdIp();
		if (match(ip,devIp)) {
			return WorkingEnvironment.DEV;
		} else if (match(ip,betaIp)) {
			return WorkingEnvironment.BETA;
		} else if (match(ip,prodIp)) {
			return WorkingEnvironment.PROD;
		}
		return null;
	}
	
	/**判断当前ip和配置的是否一样*/
	private static boolean match(String ip,String reg){
		if(ip.replaceAll(reg, "").length()==0){
			return true;
		}
		return false;
	}
	
	public static WorkingEnvironment current(){
		return current;
	}
}
